package com.xhtranslate.common.util;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * @author liuxuhuang
 * @version 1.0
 * @package com.xhtranslate.common.util
 * @date 2022/11/10 20:32:10
 * @description
 */
@Component
@Slf4j
public class HttpGetPostUtil {
    @Autowired
    private RestTemplate restTemplate;

    /**
     * 通用post请求封装
     *
     * @param url
     * @param jsonData
     * @return
     */
    public JSONObject httpPost(String url, Class<JSONObject> jsonData) {
        //RestTemplate restTemplate = new RestTemplate();
        //设置Http Header
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");
        headers.add("Accept", "application/json, text/plain, */*");
        //设置请求媒体数据类型
        headers.setContentType(MediaType.APPLICATION_JSON);
        //设置返回媒体数据类型
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        HttpEntity<String> formEntity = new HttpEntity<String>(jsonData.toString(), headers);
        return restTemplate.postForObject(url, formEntity, JSONObject.class);
    }

    /**
     * 通用get请求
     *
     * @param url
     * @return
     */
    public String httpGet(String url) {
        //设置Http Header
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");
        headers.add("Accept", "application/json, text/plain, */*");
        //设置请求媒体数据类型
        headers.setContentType(MediaType.APPLICATION_JSON);
        //设置返回媒体数据类型
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        return restTemplate.getForObject(url, String.class);
    }

    public JSONObject httpPostImg(String url, JSONObject jsonData) {
        //设置Http Header
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "multipart/form-data");
        headers.add("Accept", "application/json, text/plain, */*");
        //设置请求媒体数据类型
        headers.setContentType(MediaType.APPLICATION_JSON);
        //设置返回媒体数据类型
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        HttpEntity<String> formEntity = new HttpEntity<String>(jsonData.toString(), headers);
        return restTemplate.postForObject(url, formEntity, JSONObject.class);
    }

    ///**
    // * 自定义请求头的post请求
    // *
    // * @param url
    // * @param jsonData
    // * @param jsonHeaders
    // * @return
    // */
    //
    //public String httpPost(String url, JSONObject jsonData, JSONObject jsonHeaders) {
    //    RestTemplate restTemplate = new RestTemplate();
    //    //设置Http Header
    //    HttpHeaders headers = getHeaders(jsonHeaders);
    //    //设置请求媒体数据类型
    //    headers.setContentType(MediaType.APPLICATION_JSON);
    //    //设置返回媒体数据类型
    //    headers.add("Accept", MediaType.APPLICATION_JSON.toString());
    //    HttpEntity<String> formEntity = new HttpEntity<String>(jsonData.toString(), headers);
    //    return restTemplate.postForObject(url, formEntity, String.class);
    //}
    //
    //
    ///**
    // * 自定义请求头的get请求
    // *
    // * @param url
    // * @param jsonHeaders
    // * @return
    // */
    //public String httpGet(String url, JSONObject jsonHeaders) {
    //    RestTemplate restTemplate = new RestTemplate();
    //    HttpHeaders headers = getHeaders(jsonHeaders);
    //    HttpEntity request = new HttpEntity(headers);
    //    // 构造execute()执行所需要的参数。
    //    RequestCallback requestCallback = restTemplate.httpEntityCallback(request, JSONObject.class);
    //    ResponseExtractor<ResponseEntity<JSONObject>> responseExtractor = restTemplate.responseEntityExtractor(JSONObject.class);
    //    // 执行execute()，发送请求
    //    return Objects.requireNonNull(Objects.requireNonNull(restTemplate.execute(url, HttpMethod.GET, requestCallback, responseExtractor)).getBody()).toString();
    //}
    //
    //
    ///**
    // * json转化请求头
    // *
    // * @param jsonHeaders
    // * @return
    // */
    //public static HttpHeaders getHeaders(JSONObject jsonHeaders) {
    //    Iterator iterator = jsonHeaders.entrySet().iterator();
    //    HttpHeaders headers = new HttpHeaders();
    //    while (iterator.hasNext()) {
    //        Map.Entry entry = (Map.Entry) iterator.next();
    //        headers.add(entry.getKey().toString(), entry.getValue().toString());
    //    }
    //    return headers;
    //}

}
